package com.by.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.06.09.21.21
 */
@RestController
public class ZuulErrorController implements ErrorController {
@Autowired
    ErrorAttributes errorAttributes;
    @Override
    public String getErrorPath() {
        return "/zuulerror";
    }
    @GetMapping("/zuulerror")
    public Map error(HttpServletRequest request){
        Map<String, Object> map= errorAttributes.getErrorAttributes(new ServletWebRequest(request), ErrorAttributeOptions.defaults());
        Map rs=new HashMap();
        String msg="服务器开小差了...";
        rs.put("code",0);
        Object obj=map.get("message");
        if(null!=obj&&!"".equals(obj.toString())){
            msg=obj.toString();
        }
        rs.put("msg",msg);
        return rs;
    }
}
